import { showModal } from '@/utils/message';
import { setUserStatus } from '@/api/flyUser';
import { enter2Br } from '@/utils/string';

export default {
  data() {
    return {

    };
  },
  methods: {
    // 用户id、所设置的状态、供应商id、是否是详情页
    setUserStatus(id, status, affiliatedSuppliers, isDetail = false) {
      // if ((!Array.isArray(affiliatedSuppliers) || !affiliatedSuppliers.length) && status === 1) { // 关联供应商才能启用
      //   this.$message.warning('请先关联供应商');
      //   return;
      // }
      showModal(`请确认是否${status === 1 ? '启用' : '禁用'}用户?`, '', {
        iconClass: 'del-icon'
      }).then(() => {
        const data = {
          status,
          id
        };
        setUserStatus(data).then(res => {
          if (res.code !== 200) {
            this.$message.error(res.message);
            return;
          }
          this.$message.success(`${status === 1 ? '启用' : '禁用'}成功`);
          isDetail ? this.getUserInfo() : this.fetchData();
        }).catch(err => {
          console.log(err);
          this.$confirm(enter2Br(err.message), '', {
            confirmButtonText: '确定',
            showCancelButton: false,
            showClose: true,
            type: 'warning',
            center: true,
            iconClass: 'charge-icon',
            customClass: 'charge-message-box',
            dangerouslyUseHTMLString: true
          });
        });
      }).catch(() => {
        return false;
      });
    },
    // 发起签约
    signUpCreate(id) {
      const url = this.$router.resolve({
        name: 'signUpCreate',
        query: {
          flyUserId: id
        }
      });
      window.open(url.href, '_blank');
    },
    // 创建供应商
    createSupplier(id) {
      const url = this.$router.resolve({
        name: 'supplierManageAdd',
        query: {
          flyUserId: id
        }
      });
      window.open(url.href, '_blank');
    },
    // 关联供应商
    openDialog(id, supplierList, userName) {
      this.$nextTick(() => {
        this.$refs['supplierDialog'].openDialog(id, supplierList || [], userName);
      });
    },
    // 跳转销售供应商
    toSupplierDetail(channelId) {
      if (channelId) {
        const url = this.$router.resolve({
          name: 'supplierManageDetail',
          params: { id: channelId }
        });
        window.open(url.href, '_blank');
      }
    }
  }
};
